import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import VueRouter from 'unplugin-vue-router/vite'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { visualizer } from 'rollup-plugin-visualizer'
const host = process.env.TAURI_DEV_HOST;

// https://vitejs.dev/config/
export default defineConfig(async () => ({
  plugins: [
    vue(),
    VueRouter({
      routesFolder: 'src/views'
    }),
    AutoImport({
      resolvers: [ElementPlusResolver({
        importStyle: 'css',  // 添加这行强制包含样式
        directives: true
      })],
    }),
    Components({
      resolvers: [ElementPlusResolver({
        importStyle: 'css',  // 添加这行强制包含样式
        directives: true
      })],
    }),
    visualizer({
      open: true, // 在默认用户代理中打开生成的文件
      gzipSize: true, // 收集 gzip 大小并将其显示
      brotliSize: true, // 收集 brotli 大小并将其显示
      filename: "stats.html", // 分析图生成的文件名
    })
  ],
  build: {
    rollupOptions: {
      plugins: [
        // 如果你只想在生产构建时使用
        visualizer({
          filename: "bundle-stats.html", // 输出文件名
          title: "Bundle Visualizer", // 报告标题
          template: "treemap", // 图表类型: sunburst, treemap, network
          sourcemap: false, // 是否包含 sourcemap 数据
          projectRoot: ".", // 项目根目录
          emitFile: false, // 是否将文件写入磁盘
          open: true // 非生产环境自动打开
        })
      ]
    }
  },
  resolve: {
    alias: {
      '@': '/src'
    }
  },
  // Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
  //
  // 1. prevent vite from obscuring rust errors
  clearScreen: false,
  // 2. tauri expects a fixed port, fail if that port is not available
  server: {
    port: 1420,
    host: '0.0.0.0',
    strictPort: true,
    hmr: host
      ? {
        protocol: "ws",
        host,
        port: 1421,
      }
      : undefined,
    watch: {
      // 3. tell vite to ignore watching `src-tauri`
      ignored: ["**/src-tauri/**"],
    },
  },
}));
